<?php
namespace app\wxapplet\controller;

use think\Request;
use think\Controller;
use app\yxwadmin\controller\Admin;
use app\common\model\WechatAppletModel;

/**
* 小程序管理
*/
class Manager extends Admin{
    /**
     * 小程序管理页面
     */
    public function index(){
        return $this->view->fetch('manager/index');
    }

    /**
     * 小程序数据集合
     */
    public function getlist(){
        $limit = input('post.limit','0','trim');
        $offset = input('post.offset','0','trim');
        $sortName = input('post.sortName','id','trim');
        $sortOrder = input('post.sortOrder','asc','trim');
        $keyword = input('post.keyword','','trim');        
        
        $where['product_code'] = "photo_albums_bless";
        if(!empty($keyword)){
            $where['rule_content']=array('like','%'.$keyword.'%');
        }

        $count = model("WechatAppletModel")->where($where)->count();
        $list = model("WechatAppletModel")->where($where)->order([$sortName=>$sortOrder])->limit($offset,$limit)->select();

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }
    
    /**
     * 新增小程序
     */
    public function add(){
        if (Request::instance()->isPost()){
            $data=input('post.');
            $data['addtime'] = time();
            $data['updatetime'] = $data['addtime'];
            $data['product_code'] = 'photo_albums_bless';
            $wechatAppletModel = new WechatAppletModel();
            $res = $wechatAppletModel->addOrUp($data);
            if ($res) {
                $ret["code"]=0;
                $ret['msg']="保存成功";
                return $ret;
            }else {
                $ret["code"]=1;
                $ret['msg']="保存失败";
                return $ret;
            }
        }else{            
            return $this->view->fetch('manager/add');
        }
    }

    /**
     * 修改小程序
     */
    public function edit(){
        $wechatAppletModel = new WechatAppletModel();
        if (Request::instance()->isPost()){
            $data=input('post.');
            $data['updatetime'] = time();
            
            $res = $wechatAppletModel->addOrUp($data,array("id"=>$data["id"]));
            if ($res) {
                $ret["code"]=0;
                $ret['msg']="修改成功";
                return $ret;
            }else {
                $ret["code"]=1;
                $ret['msg']="修改失败";
                return $ret;
            }
        }else{
            $id = input('get.id');
            $data = $wechatAppletModel->getOne(["id"=>$id]);

            $this->assign('data',$data);
            return $this->view->fetch('manager/edit');
        }
    }
}